package com.webcams.core.utils;



public class Utils {
	
	public static double distance(double latA, double lonA, double latB, double lonB)  { 

		double a = Math.PI / 180; 
		double lat1 = latA * a; 
		double lat2 = latB * a; 
		double lon1 = lonA * a; 
		double lon2 = lonB * a;  
		double t1 = Math.sin(lat1) * Math.sin(lat2); 
		double t2 = Math.cos(lat1) * Math.cos(lat2); 
		double t3 = Math.cos(lon1 - lon2); 
		double t4 = t2 * t3; 
		double t5 = t1 + t4; 
		double rad_dist = Math.atan(-t5/Math.sqrt(-t5 * t5 +1)) + 2 * Math.atan(1);  
		return (rad_dist * 3437.74677 * 1.1508) * 1.6093470878864446; 
	}
	
	public static double angle(double latA, double lonA, double latB, double lonB) {
		
		double angle;
		
		double x = lonB - lonA;
		double y = latB - latA;
		
		if(x > 0 && y >= 0) {
			angle = Math.atan(y/x);
		} else if(x > 0 && y < 0) {
			angle = Math.atan(y/x) + 2 * Math.PI;
		} else if(x < 0) {
			angle = Math.atan(y/x) + Math.PI;
		} else if(x == 0 && y > 0) {
			angle = Math.PI/2;
		} else {
			angle = 3*Math.PI/2;
		}
		return 90 - Math.toDegrees(angle);

	}
	
	public static double round(double what, int howmuch) {
		return (double)( (int)(what * Math.pow(10, howmuch) + .5) ) / Math.pow(10, howmuch);
	}
	
	public static int positiveModulo(int n, int m) {
		if(n >= 0) {
			return n % m;
		} else {
			return m - (-n % m);
		}
	}
}
